package com.ningxun.rms.signup.service.impl;

import java.util.Date;
import java.util.List;
import java.util.HashMap;
import com.ningxun.rms.common.utils.DateUtils;
import com.ningxun.rms.common.utils.SecurityUtils;
import com.ningxun.rms.common.utils.bean.BeanUtils;
import com.ningxun.rms.domain.basewechatuser.BaseWechatUser;
import com.ningxun.rms.mapper.BusRequireApproveMapper;
import com.ningxun.rms.mapper.basewechatuser.BaseWechatUserMapper;
import com.ningxun.rms.signup.domain.BusSignupApprove;
import com.ningxun.rms.signup.mapper.BusSignupApproveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ningxun.rms.signup.mapper.BusSignupMapper;
import com.ningxun.rms.signup.domain.BusSignup;
import com.ningxun.rms.signup.service.IBusSignupService;
import com.ningxun.rms.signup.domain.vo.BusSignupWithRequireVO;
import org.springframework.transaction.annotation.Transactional;

import static com.ningxun.rms.common.utils.PageUtils.startPage;

/**
 * 报名信息Service业务层处理
 *
 * @author rms
 * @date 2025-07-15
 */
@Service
public class BusSignupServiceImpl implements IBusSignupService
{
    @Autowired
    private BusSignupMapper busSignupMapper;

    @Autowired
    private BusSignupApproveMapper busSignupApproveMapper;

    @Autowired
    private BaseWechatUserMapper baseWechatUserMapper;
    /**
     * 查询报名信息
     *
     * @param signupId 报名信息主键
     * @return 报名信息
     */
    @Override
    public BusSignup selectBusSignupBySignupId(Long signupId)
    {
        BusSignup busSignup = busSignupMapper.selectBusSignupBySignupId(signupId);
        if(busSignup == null){
            throw new RuntimeException("-1,没有此id");
        }
        return busSignup;
    }

    /**
     * 查询报名信息列表
     *
     * @param busSignup 报名信息
     * @return 报名信息
     */
    @Override
    public List<BusSignup> selectBusSignupList(BusSignup busSignup)
    {
        return busSignupMapper.selectBusSignupList(busSignup);
    }

    /**
     * 新增报名信息
     *
     * @param busSignup 报名信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int insertBusSignup(BusSignup busSignup)
    {
        BusSignup busSignup1 = busSignupMapper.selectSignupLatelyByUserId(SecurityUtils.getUserId());
        if(busSignup1!=null ){
                if(busSignup1.getApproveStatus()==0){
                    throw new RuntimeException("您有报名正在审核中");
                }else if(busSignup1.getApproveStatus()==1){
                    throw new RuntimeException("您有报名已通过，不能再报名了哦");
                }
        }
        // 获取当前时间
        Date nowDate = DateUtils.getNowDate();
        busSignup.setCreateTime(nowDate);
        busSignup.setUpdateTime(nowDate);
        // 设置申请状态默认为0 申请中
        busSignup.setApproveStatus(0L);
        int signupResult = busSignupMapper.insertBusSignup(busSignup);
        if (signupResult <= 0) {
            throw new RuntimeException("报名信息更新失败");
        }
        BusSignupApprove busSignupApprove = new BusSignupApprove();
        busSignupApprove.setSignupId(busSignup.getSignupId());
        int approveResult = busSignupApproveMapper.insertBusSignupApprove(busSignupApprove);
        if (approveResult <= 0) {
            throw new RuntimeException("企业插入审核更新失败");
        }
        BaseWechatUser baseWechatUser = new BaseWechatUser();
        //插入用户报名信息
        BeanUtils.copyProperties(busSignup, baseWechatUser);
        baseWechatUser.setUserId(SecurityUtils.getUserId());
        baseWechatUserMapper.updateById(baseWechatUser);
        return signupResult;
    }

    /**
     * 修改报名信息
     *
     * @param busSignup 报名信息
     * @return 结果
     */
    @Override
    public int updateBusSignup(BusSignup busSignup)
    {
        busSignup.setUpdateTime(DateUtils.getNowDate());
        return busSignupMapper.updateBusSignup(busSignup);
    }

    /**
     * 批量删除报名信息
     *
     * @param signupIds 需要删除的报名信息主键
     * @return 结果
     */
    @Override
    public int deleteBusSignupBySignupIds(Long[] signupIds)
    {
        return busSignupMapper.deleteBusSignupBySignupIds(signupIds);
    }

    /**
     * 删除报名信息信息
     *
     * @param signupId 报名信息主键
     * @return 结果
     */
    @Override
    public int deleteBusSignupBySignupId(Long signupId)
    {
        return busSignupMapper.deleteBusSignupBySignupId(signupId);
    }

    @Override
    public Integer selectIsSuccess(String openId) {

        BaseWechatUser baseWechatUser = baseWechatUserMapper.selectBaseWechatUserByOpenId(openId);
       if(baseWechatUser == null){
           throw new RuntimeException("请先填写用户信息");
       }
        BusSignup busSignup = busSignupMapper.selectSignupLatelyByUserId(SecurityUtils.getUserId());
        if(busSignup == null||busSignup.getApproveStatus()==-1){
            return -1;
        }
        if(busSignup.getApproveStatus() == 1){
            return 1;
        }else if(busSignup.getApproveStatus() == 2){
            return 2;
        }
         return 0;
    }

    /**
     * 查询报名信息与需求信息联查列表
     *
     * @param busSignup 报名信息
     * @return 报名信息与需求信息联查集合
     */
    @Override
    public List<BusSignupWithRequireVO> selectSignupWithRequireList(BusSignup busSignup)
    {
        // 只有在查询列表时才分页，导出时不分页
        if (busSignup.getParams() != null && busSignup.getParams().containsKey("export")) {
            return busSignupMapper.selectSignupWithRequireList(busSignup);
        } else {
            startPage();
            return busSignupMapper.selectSignupWithRequireList(busSignup);
        }
    }

    /**
     * 导出报名信息数据
     *
     * @param busSignup 报名信息查询条件
     * @return 报名信息数据集合
     */
    @Override
    public List<BusSignupWithRequireVO> exportSignupData(BusSignup busSignup)
    {
        // 导出时不进行分页，返回所有符合条件的数据
        if (busSignup.getParams() == null) {
            busSignup.setParams(new HashMap<>());
        }
        busSignup.getParams().put("export", true);
        return busSignupMapper.selectSignupWithRequireList(busSignup);
    }
}
